package org.adream.account.service;

import org.adream.account.dao.DonornDreamCenterLinkDao;
import org.adream.account.dao.DreamCenterDao;
import org.adream.account.entity.DonornDreamCenterLinkEntity;
import org.adream.account.entity.DreamCenterEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class DreamCenterService {
	
	@Autowired
	private DreamCenterDao dreamCenterDao;
	
	@Autowired
	private DonornDreamCenterLinkDao donornDreamCenterLinkDao;
	
	private final static Logger logger = LoggerFactory.getLogger(DreamCenterService.class);
	
	/**
	 * 添加或更新梦想中心
	 * @param dreamCenterEntity
	 * @return
	 */
	public ResultModel<?> addOrUpdateDreamCenter(DreamCenterEntity dreamCenterEntity) {
		DreamCenterEntity dreamCenter = dreamCenterDao.queryDreamCenterById(dreamCenterEntity.getDcid());
		if(dreamCenter == null) {
			int addDreamCenterResult = dreamCenterDao.addDreamCenter(dreamCenterEntity);
			if(addDreamCenterResult == Constant.ZERO) {
				logger.warn("添加梦想中心失败,数据库错误:(");
				return new ResultModel<String>(ResultModel.ERR_DATABASE, "添加梦想中心失败,请联系管理员", null);
			}
			return new ResultModel<String>(ResultModel.SUCCESS, "添加梦想中心成功", null);
		}
		Utils.copyPropertiesIgnoreNull(dreamCenterEntity, dreamCenter);
		dreamCenter.setConfirmDate(dreamCenterEntity.getConfirmDate());
		dreamCenterDao.updateDreamCenterById(dreamCenter);
		return new ResultModel<String>(ResultModel.SUCCESS, "更新梦想中心成功", null);
	}
	
	/**
	 * 添加或更新捐赠方-梦想中心
	 * @param donornDreamCenterLinkEntity
	 * @return
	 */
	public ResultModel<?> addOrUpdateDonornDreamCenter(DonornDreamCenterLinkEntity donornDreamCenterLinkEntity) {
		DonornDreamCenterLinkEntity donornDreamCenter = donornDreamCenterLinkDao.queryDonornDreamCenterById(donornDreamCenterLinkEntity.getDonornId(), donornDreamCenterLinkEntity.getDcid());
		if(donornDreamCenter == null) {
			int addDonornDreamCenterResult = donornDreamCenterLinkDao.addDonornDreamCenter(donornDreamCenterLinkEntity);
			if(addDonornDreamCenterResult == Constant.ZERO) {
				logger.warn("添加捐赠方-梦想中心失败,数据库错误:(");
				return new ResultModel<String>(ResultModel.ERR_DATABASE, "添加捐赠方-梦想中心失败,请联系管理员", null);
			}
			return new ResultModel<String>(ResultModel.SUCCESS, "添加捐赠方-梦想中心成功", null);
		}
		donornDreamCenter.setDr(donornDreamCenterLinkEntity.getDr());
		donornDreamCenterLinkDao.updateDonornDreamCenterById(donornDreamCenter);
		return new ResultModel<String>(ResultModel.SUCCESS, "更新捐赠方-梦想中心成功", null);
	}
}